/*
 * File     : MaskBitFieldHelperImpl.java
 * Created  : 1 Jan 2012
 *
 * Copyright © 2012 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal;

import java.util.Map;
import java.util.Set;

import com.googlecode.dni.type.cenum.MaskBitFieldHelper;

/**
 * <p>
 *  Implementation of {@link MaskBitFieldHelper}.
 * </p>
 *
 * @param <E>
 *            the enum type
 *
 * @author Matthew Wilson
 */
final class MaskBitFieldHelperImpl< E extends Enum< E > > implements MaskBitFieldHelper< E >
{

    private final EnumDictionary dictionary;

    private final int offset;

    /**
     * @param type
     *            the type
     * @param offset
     *            the offset
     */
    MaskBitFieldHelperImpl( final Class< E > type,
                            final int offset )
    {
        this.offset = offset;
        this.dictionary = EnumDictionary.get( type );
    }

    /** {@inheritDoc} */
    @Override
    public int withValue( final int field, final E enumValue, final boolean set )
    {
        int value = this.dictionary.valueForEnum( enumValue ) << this.offset;
        return set ? ( field | value ) : ( field & ~value );
    }

    /** {@inheritDoc} */
    @Override
    public int withSet( final int field, final E enumValue )
    {
        int value = this.dictionary.valueForEnum( enumValue ) << this.offset;
        return field | value;
    }

    /** {@inheritDoc} */
    @Override
    public int withClear( final int field, final E enumValue )
    {
        int value = this.dictionary.valueForEnum( enumValue ) << this.offset;
        return field & ~value;
    }

    /** {@inheritDoc} */
    @Override
    public boolean isSet( final int field, final E enumValue )
    {
        int value = this.dictionary.valueForEnum( enumValue ) << this.offset;
        return ( field & value ) != 0;
    }

    /** {@inheritDoc} */
    @Override
    public String toString( final int field )
    {
        StringBuilder str = new StringBuilder();

        // something weird with generics is going on here; I'm confused
        Set< ? > entrySet = this.dictionary.getEnumToValueMap().entrySet();
        for ( final Object object : entrySet )
        {
            Map.Entry< ?, ? > entry = (Map.Entry< ?, ? >) object;
            Object enumValue = entry.getKey();
            int value = (Integer) entry.getValue();

            if ( ( field & value ) != 0 )
            {
                if ( str.length() > 0 )
                {
                    str.append( ',' );
                }
                str.append( enumValue );
            }
        }

        return str.toString();
    }

}
